package com.cyw.common.utils;


import com.cyw.common.exceptions.ParamCheckException;

/**
 * 正则相关校验的工具类
 *
 * @Author:cyw
 * @CreateTime: 2021/1/18 15:15
 **/
public class RegexUtils {

    /**
     * 正则表达式 - 所有字符必须为英文字母
     */
    public static final String ALL_ENGLISH_LETTER = "[a-zA-Z]+";

    /**
     * 正则表达式 - 所有字符必须为数字
     */
    public static final String ALL_NUMBER = "^[0-9]*$";

    /**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_MOBILE = "^((14[0-9])|(19[0-9])|(16[0-9])|(17[0-9])|(13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$";

    /**
     * url正则
     */
    public static final String REGEX_URL = "(?:https?://)?[\\w]{1,}(?:\\.?[\\w]{1,})+[\\w-_/?&=#%:]*$";

    /**
     * 13位时间戳正则
     */
    private static final String REGEX_TIMESTAMP_MILLI = "^([0-9]{13})$";

    /**
     * 判断字符串是否为13位数字时间戳
     *
     * @param str 字符串
     * @return 是否
     */
    public static boolean isTimeStampMilli(String str) {
        return BaseUtils.isEmpty(str) ? false : str.matches(REGEX_TIMESTAMP_MILLI);
    }

    /**
     * 所有字符都必须为英文字母
     *
     * @param str 字符串[内部会进行trim操作]
     * @return 参数为空串 或者 null 返回false
     */
    public static Boolean allEnglishLetter(String str) {

        return BaseUtils.ofNullable(str)
                .map(String::trim)
                .map(s -> s.matches(ALL_ENGLISH_LETTER))
                .orElse(false);
    }

    /**
     * 校验url
     */
    public static void checkUrl(String url) {
        if (BaseUtils.isNotBlank(url) && !url.matches(REGEX_URL)) {
            throw new ParamCheckException("url格式异常");
        }
    }

    public static void main(String[] args) {
//        System.out.println(allEnglishLetter("asdhakshdkajshd"));
//        System.out.println(allEnglishLetter("         asdhakshdkajshd"));
//        System.out.println(allEnglishLetter("a12312"));
//        System.out.println(allEnglishLetter("a12312            "));
        System.out.println("1asdasd".matches(ALL_NUMBER));
        System.out.println("123123".matches(ALL_NUMBER));
        System.out.println("12   3123".matches(ALL_NUMBER));
        System.out.println(" 1231231  ".matches(ALL_NUMBER));


    }

}

    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    